package study.android.chajajo;

import java.util.List;
import java.util.Map;

import study.android.chajajo.parser.NaverCafe;
import android.util.Log;

public class Worker implements Runnable {

	/**
	 * 
	 */
	private static final String LOG_TAG = "ChaJaJo_Worker";
	String keyword;
	int milliInterval;
	int max = 100;

	public Worker(String keyword, int sec) {
		super();
		this.keyword = keyword;
		this.milliInterval = sec * 1000;
	}

	public void run() {
		int i = 0;
		while (true) {
			try {
				Thread.sleep(milliInterval);
				Log.d(LOG_TAG, "run " + i++);
				List<Map<String, String> > list = NaverCafe.parse(NaverCafe.JOONGONARA, NaverCafe.JOONGONARA_PHONE, 1);
				
				for(Map<String, String> map: list){
					String title = map.get(NaverCafe.DATA_KEY_TITLE);
					if(title == null || !title.contains(keyword)){
						Log.d(LOG_TAG, "not found");
						continue;
					}
					
					Log.d(LOG_TAG, "name:" + map.get(NaverCafe.DATA_KEY_NAME));
					Log.d(LOG_TAG, "title:" + map.get(NaverCafe.DATA_KEY_TITLE));
					Log.d(LOG_TAG, "read:" + map.get(NaverCafe.DATA_KEY_READ));
					Log.d(LOG_TAG, "time:" + map.get(NaverCafe.DATA_KEY_TIME));
					Log.d(LOG_TAG, "link_url:" + map.get(NaverCafe.DATA_KEY_LINK_URL));
				}
				
			} catch (InterruptedException e) {
				Log.d(LOG_TAG, "interrupted! stop!");
				break;
			} catch (Exception e) {
				Log.d(LOG_TAG, "interrupted! stop!");
				break;
			}
			if (i > max) {
				Log.d(LOG_TAG, "max(" + max + ") reached. stop!");
			}
		}
	}

}
